<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if (!is_null($this->layout)) {
            $this->layout = View::make($this->layout);
        }
    }

    /**
     * 返回成功状态的json数据
     * @param null $data 返回的data
     * @param null $message 消息
     * @return mixed json对象
     */
    protected function success($data=null, $message='成功')
    {
        return response()->json(array(
            'status' => RESPONSE_SUCCESS,
            'code'=>'',
            'message' => $message,
            'auth_failure' => AUTH_FAILURE_NO,
            'data' => $data

        ), 200);
    }

    /**
     * 返回失败状态的json数据
     * @param null $message 消息
     * @param null $data 返回的data
     * @return mixed json对象
     */
    protected function failure($message, $data=null)
    {
        if (!$message)
            $message = 'Request failed!';
        if (!is_string($message))
            $message = $message->first();

        return response()->json(array(
            'status' => RESPONSE_FAILURE,
            'code'=>'',
            'message' => $message,
            'auth_failure' => AUTH_FAILURE_NO,
            'data' => $data
        ), 200);
    }

    /**
     * 返回失败状态的json数据
     * @param null $message 消息
     * @param null $data 返回的data
     * @return mixed json对象
     */
    protected function args_invalid()
    {
        return response()->json(array(
            'status' => RESPONSE_ARGUMENT_INVALID,
            'code'=>'',
            'message' => '非法参数',
            'auth_failure' => AUTH_FAILURE_NO,
            'data' => []
        ), 200);
    }

    /**
     * 自定义状态的json对象
     * @param $status 状态值
     * @param $message 消息
     * @param $data 返回数据
     * @return mixed json对象
     */
    protected function jsonResult($status, $message, $data,$code,$auth_failure)
    {
        return response()->json(array(
            'status' => $status,
            'code'=>$code,
            'message' => $message,
            'auth_failure' => $auth_failure,
            'data' => $data
        ));
    }

    /**
     * 返回失败状态的json数据
     * @param null $message 消息
     * @param null $data 返回的data
     * @return mixed json对象
     */
    protected function need_bind()
    {
        return response()->json(array(
            'status' => RESPONSE_FAILURE,
            'code'=>MESSAGECODE_USER_NOT_BIND_PHONE,
            'message' => '未绑定手机号码,无法购买',
            'auth_failure' => AUTH_FAILURE_NO,
            'data' => []
        ), 200);
    }

    /**
     * 计算最大单位表示的尺寸
     * @param $size 原始字节数,如65536**参数$digits,保留的小数位数,默认为2位
     * @param int $digits
     * @return string 最大单位表示的尺寸
     */
    public function calc($size, $digits = 2)
    {
        $unit = array('', 'K', 'M', 'G', 'T', 'P');
        $base = 1024;
        $i = floor(log($size, $base));
        $n = count($unit);
        if ($i >= $n) {
            $i = $n - 1;
        }
        return round($size / pow($base, $i), $digits) . ' ' . $unit[$i] . 'B';
    }
}
